from php4dvd.model.user import User
from selenium.webdriver.common.keys import Keys
from php4dvd.model.movie import Movie
from php4dvd.pages.internal_page import InternalPage
from php4dvd.pages.login_page import LoginPage
from php4dvd.pages.user_management_page import UserManagementPage
from php4dvd.pages.user_profile_page import UserProfilePage
from php4dvd.pages.home_page import HomePage
from php4dvd.pages.movie_page import MoviePage
from php4dvd.pages.movie_adding_page import MovieAddingPage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import *


class Application(object):
    def __init__(self, driver, base_url):
        self.driver = driver
        driver.get(base_url)
        self.wait = WebDriverWait(driver, 10)
        self.login_page = LoginPage(driver, base_url)
        self.internal_page = InternalPage(driver, base_url)
        self.user_profile_page = UserProfilePage(driver, base_url)
        self.user_management_page = UserManagementPage(driver, base_url)
        self.home_page = HomePage(driver, base_url)
        self.movie_adding_page = MovieAddingPage(driver, base_url)
        self.movie_page = MoviePage(driver, base_url)

    def logout(self):
        self.internal_page.logout_button.click()
        self.wait.until(alert_is_present()).accept()

    def ensure_logout(self):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            self.logout()

    def login(self, user):
        lp = self.login_page
        lp.is_this_page
        lp.username_field.send_keys(user.username)
        lp.password_field.send_keys(user.password)
        lp.submit_button.click()

    def ensure_login_as(self, user):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            # we are on internal page
            if self.is_logged_in_as(user):
                return
            else:
                self.logout()
        self.login(user)

    def is_logged_in(self):
        return self.internal_page.is_this_page

    def is_logged_in_as(self, user):
        return self.is_logged_in() \
            and self.get_logged_user().username == user.username

    def is_not_logged_in(self):
        return self.login_page.is_this_page

    def get_logged_user(self):
        self.internal_page.user_profile_link.click()
        upp = self.user_profile_page
        upp.is_this_page
        return User(username=upp.user_form.username_field.get_attribute("value"),
                    email=upp.user_form.email_field.get_attribute("value"))

    def add_user(self, user):
        self.internal_page.user_management_link.click()
        ump = self.user_management_page
        ump.is_this_page
        ump.user_form.username_field.send_keys(user.username)
        ump.user_form.email_field.send_keys(user.email)
        ump.user_form.password_field.send_keys(user.password)
        ump.user_form.password1_field.send_keys(user.password)
        #ump.user_form.role_select.select_by_visible_text(user.role)
        ump.user_form.submit_button.click()

    def add_movie(self, movie):
        self.internal_page.home_page_link.click()
        hp = self.home_page
        hp.is_this_page
        hp.add_movie_button.click()
        ma = self.movie_adding_page
        ma.is_this_page
        ma.movie_name_field.send_keys(movie.name)
        ma.movie_year_field.send_keys(movie.year)
        ma.save_description_button.click()

    def is_movie_was_added(self, movie):
        assert self.find_movie(movie)

    def is_movie_was_not_added(self, movie):
        assert not self.find_movie(movie)

    def find_movie(self, movie):
        self.internal_page.home_page_link.click()
        hp = self.home_page
        hp.is_this_page
        search = hp.search_input_field
        search.clear()
        search.send_keys(movie.name)
        search.send_keys(Keys.ENTER)
        self.driver.set_page_load_timeout(10)
        foundElements = hp.find_movies()
        if len(foundElements) == 0:
            return False
        return True

    def is_search_was_successful(self, movie):
        assert self.find_movie(movie)

    def is_search_was_not_successful(self, movie):
        assert not self.find_movie(movie)

    def ensure_there_are_movies(self, movie):
        self.add_movie(movie)
        self.is_movie_was_added(movie)

    def delete_any_movie(self):
        self.internal_page.home_page_link.click()
        hp = self.home_page
        hp.is_this_page
        filmId = hp.go_first_movie_on_the_page()
        mp = self.movie_page
        mp.is_this_page
        mp.remove_this()
        return filmId

    def is_movie_deleted(self, movieId):
        self.driver.set_page_load_timeout(10)
        self.internal_page.home_page_link.click()
        hp = self.home_page
        hp.is_this_page
        elementsAfterDelete = hp.find_movies()
        fl = [el for el in elementsAfterDelete if el.get_attribute("id") == movieId]
        assert len(fl) == 0


